package com.mazaiting.redeye.module.splash

import android.text.TextUtils
import com.mazaiting.easy.base.bean.BaseBean
import com.mazaiting.easy.base.mvp.IBasePresenter
import com.mazaiting.easy.base.mvp.IBaseView
import com.mazaiting.easy.base.presenter.BaseNetPresenter
import com.mazaiting.easy.utils.rx.RxScheduler
import com.mazaiting.log.L
import com.mazaiting.redeye.api.UserApi
import com.mazaiting.redeye.base.*
import com.trello.rxlifecycle3.kotlin.bindToLifecycle
import dagger.Component
import io.reactivex.disposables.Disposable
import javax.inject.Inject

/***
 *
 *
 *                                                    __----~~~~~~~~~~~------___
 *                                   .  .   ~~//====......          __--~ ~~
 *                   -.            \_|//     |||\\  ~~~~~~::::... /~
 *                ___-==_       _-~o~  \/    |||  \\            _/~~-
 *        __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
 *    _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
 *  .~       .~       |   \\ -_    /  /-   /   ||      \   /
 * /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
 * |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
 *          '         ~-|      /|    |-~\~~       __--~~
 *                      |-~~-_/ |    |   ~\_   _-~            /\
 *                           /  \     \__   \/~                \__
 *                       _--~ _/ | .-~~____--~-/                  ~~==.
 *                      ((->/~   '.|||' -_|    ~~-/ ,              . _||
 *                                 -_     ~\      ~~---l__i__i__i--~~_/
 *                                 _-~-__   ~)  \--______________--~~
 *                               //.-~~~-~_--~- |-------~~~~~~~~
 *                                      //.-~~~--\
 *                               神兽保佑
 *                              代码无BUG!
 * @author mazaiting
 * @date 2019-06-21
 * @decription 开屏相关
 */
/**
 * 开屏联系人
 */
class SplashContract {
    interface View : IBaseView {}
    interface Presenter : ICancelPresenter<View> {
        /**
         * 获取公钥
         */
        fun publicKey()

        /**
         * 检测是否有公钥，如果有则不去请求
         * @return true: 需要请求; false: 不需要请求
         */
        fun checkPublicKey(): Boolean

        /**
         * 检测是否登录
         * @return true: 已登录; false: 未登录
         */
        fun checkLogin(): Boolean
    }
}

/**
 * 开屏主持人
 */
class SplashPresenter
@Inject constructor(
    private val userApi: UserApi
) : BaseCommonPresenter<SplashContract.View>(), SplashContract.Presenter {

    /**
     * 登录 Disposable
     */
    private var mPublicKeyDispose: Disposable? = null

    override fun publicKey() {
        mPublicKeyDispose = userApi.publicKey()
            .compose(RxScheduler.applySchedulers())
            .bindToLifecycle(view.lifecycleProvider)
            .subscribe(object : CustomConsumer<BaseBean> {
                override fun onSuccess(result: BaseBean?) {
                    if (null != result) {
                        // 存储公钥
                        putString(Config.KEY_PUBLIC_KEY, result.msg)
                    }
                }
            })
    }

    override fun checkPublicKey(): Boolean {
        // 获取公钥
        val publicKey = getString(Config.KEY_PUBLIC_KEY)
        L.d("公钥: $publicKey")
        // 检测是否为空
        return TextUtils.isEmpty(publicKey)
    }

    override fun checkLogin(): Boolean = !TextUtils.isEmpty(username) && !TextUtils.isEmpty(token)

    override fun cancel() {
        mPublicKeyDispose?.dispose()
    }
}

/**
 * 开屏页组件
 */
@Component(dependencies = [ApplicationComponentImpl::class])
interface SplashComponent {
    /**
     * 注入页面
     * @param activity 页面
     */
    fun inject(activity: SplashActivity)
}
